//
//  QualityInspectionVC.m
//  YaoHeAPP
//
//  Created by mac on 16/1/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "QualityInspectionVC.h"
#import "QualityInspection.h"
#import "QualityCell.h"
#import "Controller.h"
#import "Define.h"
#import "Masonry.h"
#import "ProvinceAndCity.h"
#import "QualityHeadView.h"
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height

@interface QualityInspectionVC ()<UITableViewDataSource,UITableViewDelegate,QualityCellDelegate,QualityHeadViewDelegate>
{
    QualityCell*_cell;//表格
    UIButton*selectAll;//全选按钮
//    UIButton*quTouBtn;//区头选择按钮
    UIButton*applyQualityBtn;//申请质检
    NSInteger  zhiJianCount;//质检个数
    UILabel*yiXuanLab;//显示已选多少个
    BOOL  selectQuTou;//是否选择区头
}
@end

@implementation QualityInspectionVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"QualityInspectionVC"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"QualityInspectionVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=bgVCcolor;
    _titleAry=[[NSMutableArray alloc]init];
    _dataAry= [[NSMutableArray alloc]init];
    _orderIDArray=[[NSMutableArray alloc]init];
    _quTouArray=[[NSMutableArray alloc]init];
    
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);        make.left.offset(0);
        make.height.offset(60);    make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"申请质检";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);      make.left.offset(screenWidth/2-50);
        make.width.offset(100);   make.height.offset(30);
    }];
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);      make.width.offset(24);
        make.height.offset(22);   make.bottom.offset(-12);
    }];
    
    NSDictionary*dic=@{@"":@""};
    [Controller requestWithURL:@"AppSaleOrder/PostQueryNeedTestOrder?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            NSArray*shuJuAry=classData.data;
            for (NSInteger i=0; i<shuJuAry.count; i++) {
                NSMutableArray*xiangQingAry=[[NSMutableArray alloc]init];
                NSDictionary*dic=shuJuAry[i];
                ZhiJianIsNeed*zhiJian=[[ZhiJianIsNeed alloc]initWithDid:dic];
                [_titleAry addObject:zhiJian];
                
                for (NSInteger a=0; a<zhiJian.orderInfosAry.count; a++) {
                    NSDictionary*orderDetailsDic=zhiJian.orderInfosAry[a];
                    QualityInspection*quality=[[QualityInspection alloc]initWithDid:orderDetailsDic];
                    [xiangQingAry addObject:quality];
                }
                NSDictionary*duiXingDic=@{zhiJian.companyName:xiangQingAry};
                [_dataAry addObject:duiXingDic];
            }
            //创建表格
            _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, screenWidth, screenHeight-120) style:UITableViewStylePlain];
            _tableView.delegate=self;   _tableView.dataSource=self;
            [self.view addSubview:_tableView];
            [_tableView registerClass:[QualityCell class] forCellReuseIdentifier:@"Cell"];
            UIView*bottomView=[[UIView alloc]init];
            bottomView.backgroundColor=[UIColor whiteColor];
            [self.view addSubview:bottomView];
            [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(0);        make.height.offset(60);
                make.right.offset(0);       make.bottom.offset(0);
            }];
            UIImageView*lines=[[UIImageView alloc]init];
            lines.backgroundColor=[UIColor colorWithRed:200.f/255.f green:200.f/255.f blue:200.f/255.f alpha:1];
            [bottomView addSubview:lines];
            [lines mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(0);    make.height.offset(0.5);
                make.right.offset(0);    make.top.offset(0);
            }];
            //全选的按钮
            selectAll=[UIButton buttonWithType:UIButtonTypeCustom];
            [selectAll setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
            
            [selectAll addTarget:self action:@selector(clickSelectAll) forControlEvents:UIControlEventTouchUpInside];
            [bottomView addSubview:selectAll];
            [selectAll mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(10);      make.top.offset(20);
                make.height.offset(20);   make.width.offset(20);
            }];
            UILabel*quanXuan=[[UILabel alloc] init];
            quanXuan.text=@"全选";  quanXuan.font=font14;
            quanXuan.textColor=blackColor51;
            [bottomView addSubview:quanXuan];
            [quanXuan mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(38);     make.height.offset(18);
                make.top.offset(21);
            }];
            //申请质检
            applyQualityBtn=[[UIButton alloc]init];
            applyQualityBtn.frame=CGRectMake(screenWidth-100, 0, 100, 60);
            if (_orderIDArray.count>0) {
                 applyQualityBtn.backgroundColor=[UIColor colorWithRed:54.f/255.f green:177.f/255.f blue:248.f/255.f alpha:1];
            }else{
                applyQualityBtn.backgroundColor=[UIColor colorWithRed:196.f/255.f green:196.f/255.f blue:196.f/255.f alpha:1];
            }
            [applyQualityBtn setTitle:@"申请质检" forState:UIControlStateNormal];
            [applyQualityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [applyQualityBtn  addTarget:self action:@selector(applyQuality) forControlEvents:UIControlEventTouchUpInside];
            [bottomView addSubview:applyQualityBtn];
            //已选个数
            yiXuanLab=[[UILabel alloc]init];
            yiXuanLab.font=font14;   yiXuanLab.textColor=blackColor51;
            yiXuanLab.text=[NSString stringWithFormat:@"已选: x%ld",(unsigned long)_orderIDArray.count];
            [bottomView addSubview:yiXuanLab];
            [yiXuanLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(applyQualityBtn.mas_left).offset(-18);
                make.top.offset(21);    make.height.offset(18);
            }];
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];
}

#pragma mark------------单元格tableView的代理协议方法
//返回有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataAry.count;
}

//返回每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ZhiJianIsNeed*zhijian=_titleAry[section];
    NSArray*countAry=_dataAry[section][zhijian.companyName];
    return countAry.count;
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 26;
}
//返回每一行单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 106;
}
//设置cell上的要显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    _cell=[[QualityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    _cell.selectionStyle=UITableViewCellSelectionStyleNone;
    _cell.delegate=self;
    ZhiJianIsNeed*isNeed=_titleAry[indexPath.section];
    NSArray*neiRongAry=_dataAry[indexPath.section][isNeed.companyName];
    QualityInspection*quaIns=neiRongAry[indexPath.row];
    _cell.quality=quaIns;
    return _cell;
}

//返回区头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    QualityHeadView*headView=[[QualityHeadView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 26)];
    [headView.quTouBtn addTarget:self action:@selector(companySelect:) forControlEvents:UIControlEventTouchUpInside];
    ZhiJianIsNeed*isNeed=_titleAry[section];
    NSMutableArray*ary=_dataAry[section][isNeed.companyName];
    if ([isNeed.trueANDfalse isEqualToString:@"true"]) {
        [headView.quTouBtn setBackgroundImage:[UIImage imageNamed:@"ic_apply_quality_selected"] forState:UIControlStateNormal];
        for (NSInteger i; i<ary.count; i++) {
            QualityInspection*quality=ary[i];
            [quality setValue:@"true" forKey:@"trueAndfalse"];
        }
    }else{
        [headView.quTouBtn setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
    }
    headView.quTouBtn.tag=section+100;
    headView.companyLabel.text=isNeed.companyName;
    return headView;
}

//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark---------------------自己写的单元格的代理方法
-(void)QualityCellDelegate:(QualityCell *)cell WithBtn:(UIButton *)btn
{
    
    NSIndexPath *indexPath =[_tableView indexPathForCell:cell];
    ZhiJianIsNeed*zhijian=_titleAry[indexPath.section];
    NSArray*neiRongAry=_dataAry[indexPath.section][zhijian.companyName];
    NSInteger  countOrderID=0;
    for (NSInteger i=0; i<neiRongAry.count;i++) {
        QualityInspection*quaIns=neiRongAry[i];
        if (i==indexPath.row) {
            if (btn.selected==YES) {
                [_orderIDArray addObject:quaIns.orderID];
                [quaIns setValue:@"true" forKey:@"trueAndfalse"];
            }else{
                [_orderIDArray removeObject:quaIns.orderID];
                [quaIns setValue:@"false" forKey:@"trueAndfalse"];
            }
        }
        if ([quaIns.trueAndfalse isEqualToString:@"true"]) {
            countOrderID++;
        }else{
            countOrderID--;
        }
    }
    if (countOrderID==neiRongAry.count) {
        [zhijian setValue:@"true" forKey:@"trueANDfalse"];
    }else { [zhijian setValue:@"false" forKey:@"trueANDfalse"]; }
    yiXuanLab.text=[NSString stringWithFormat:@"已选: x%ld",_orderIDArray.count];
    if (_orderIDArray.count>0) {applyQualityBtn.backgroundColor=[UIColor colorWithRed:54.f/255.f green:177.f/255.f blue:248.f/255.f alpha:1];}
    else{applyQualityBtn.backgroundColor=[UIColor colorWithRed:196.f/255.f green:196.f/255.f blue:196.f/255.f alpha:1];}
    [_tableView reloadData];
}

#pragma mark---------------------区头的代理方法
-(void)headerViewdelegate:(QualityHeadView *)header AndBtn:(UIButton *)btn
{
    
}

#pragma mark---------------------按钮点击事件
//区头公司名的点击事件
-(void)companySelect:(UIButton*)quTou
{
    ZhiJianIsNeed*isNeed=_titleAry[quTou.tag-100];
    NSMutableArray*ary=_dataAry[quTou.tag-100][isNeed.companyName];
    if ([isNeed.trueANDfalse isEqualToString:@"true"]) {
        [isNeed setValue:@"false" forKey:@"trueANDfalse"];
        for (NSInteger i; i<ary.count; i++) {
            QualityInspection*quality=ary[i];
            [quality setValue:@"false" forKey:@"trueAndfalse"];
        }
    }else{
        [isNeed setValue:@"true" forKey:@"trueANDfalse"];
        [quTou setBackgroundImage:[UIImage imageNamed:@"ic_apply_quality_selected"] forState:UIControlStateNormal];
        for (NSInteger i; i<ary.count; i++) {
            QualityInspection*quality=ary[i];
            [quality setValue:@"true" forKey:@"trueAndfalse"];
        }
        
    }
    [_tableView reloadData];
}
//点击按钮全选
-(void)clickSelectAll
{
    
}
//申请质检的按钮
-(void)applyQuality
{
    
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
